﻿Imports System.Collections.Generic
Imports System.Data
Imports Biz
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos para el manejo de la entidad Password
''' </summary>
''' <remarks></remarks>
Public Class DALPassword

    Private Shared m_Instance As DALPassword
    Private Shared m_Semaforo As New Object
    Private m_TDGPassword As TDGPassword


    Public Function Insert(obj As cPassword) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGPassword.Insert(database.Conexion, obj.Usu_Legajo, obj.Pwd_Numero, obj.Pwd_Password, obj.Pwd_Fecha, obj.Pwd_FechaCaducacion, obj.DVH)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.PasswordFalloAlta(ex)
        End Try

    End Function

    Public Function Sel_Ultimo(Usu_Legajo As Int16) As cPassword
        Dim dr As SqlDataReader
        Dim obj As cPassword
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGPassword.Find_PorUsuario_Ultimo(database.Conexion, Usu_Legajo)

            If dr.HasRows Then

                obj = New cPassword

                dr.Read()

                obj.Usu_Legajo = Utils.Convert.ToInt16(dr.GetValue(0))
                obj.Pwd_Numero = Utils.Convert.ToInt16(dr.GetValue(1))
                obj.Pwd_Password = Utils.Convert.ToStr(dr.GetValue(2))
                obj.Pwd_Fecha = Utils.Convert.ToDate(dr.GetValue(3))
                obj.Pwd_FechaCaducacion = Utils.Convert.ToDate(dr.GetValue(4))
                obj.DVH = Utils.Convert.ToInt32(dr.GetValue(5))

                dr.Close()

            Else

                obj = Nothing

            End If

            database.Desconectar()

            Return obj
        Catch ex As Exception
            Throw New Excepciones.PasswordFalloBuscarUltimo(ex)
        End Try
    End Function

    Public Function Sel_PorUsuario(Usu_Legajo As Int16) As List(Of cPassword)
        Dim dr As SqlDataReader
        Dim obj As cPassword
        Dim database As cDatabase

        Sel_PorUsuario = New List(Of cPassword)

        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGPassword.Find_PorUsuario_Ultimo(database.Conexion, Usu_Legajo)

            If dr.HasRows Then

                Do While dr.Read

                    obj = New cPassword

                    obj.Usu_Legajo = Utils.Convert.ToInt16(dr.GetValue(0))
                    obj.Pwd_Numero = Utils.Convert.ToInt16(dr.GetValue(1))
                    obj.Pwd_Password = Utils.Convert.ToStr(dr.GetValue(2))
                    obj.Pwd_Fecha = Utils.Convert.ToDate(dr.GetValue(3))
                    obj.Pwd_FechaCaducacion = Utils.Convert.ToDate(dr.GetValue(4))
                    obj.DVH = Utils.Convert.ToInt32(dr.GetValue(5))

                    Sel_PorUsuario.Add(obj)

                Loop

                dr.Close()

            Else

                Sel_PorUsuario.Clear()

            End If

            database.Desconectar()

            Return Sel_PorUsuario
        Catch ex As Exception
            Throw New Excepciones.PasswordFalloBuscarPorUsuario(ex)
        End Try
    End Function

    Public Shared Function Instance() As DALPassword
        If m_Instance Is Nothing Then
            SyncLock (m_Semaforo)
                If m_Instance Is Nothing Then
                    m_Instance = New DALPassword
                End If
            End SyncLock
        End If

        Return m_Instance
    End Function

    Private Sub New()
        m_TDGPassword = TDGPassword.Instance()
    End Sub

End Class
